package sanrenxing.dayEnd.backRepertoryTotal.utils;




import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sanrenxing.dayEnd.backRepertoryTotal.domain.logMsg;
import sanrenxing.dayEnd.backRepertoryTotal.domain.users;
import sanrenxing.dayEnd.backRepertoryTotal.service.impl.LogmsgIm;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
@Aspect
public class ProxyAll {
    @Resource
    private LogmsgIm logmsgIm;
    private logMsg lm=new logMsg();
    @Pointcut("execution(* sanrenxing.dayEnd.backRepertoryTotal.controller..*.*(..))")
    private void myProxyAll(){}
    @Before("myProxyAll()")
    public void mybe(JoinPoint point){
        ServletRequestAttributes sra= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest ht=sra.getRequest();
        HttpSession se=ht.getSession();
        users us= (users) se.getAttribute("sn");
        Date dt=new Date();
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        lm.setVisitTime(sf.format(dt.getTime()));
        //lm.setIpAddress(ht.getRemoteAddr());
        lm.setIpAddress(getIp(ht));


        System.out.println(point.getTarget().getClass().getName());
        lm.setExecWay(point.getSignature().getName());
        lm.setVisitURL(ht.getRequestURI());
        lm.setExecTime(dt.getTime());
        ht.setAttribute("lmsg",lm);
        System.out.println(lm.getVisitTime());
        System.out.println(lm.getIpAddress());
        System.out.println(lm.getExecWay());
        System.out.println(lm.getVisitURL());
    }
    @After(value = "myProxyAll()")
    public void myaf(JoinPoint point){
        ServletRequestAttributes sra= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest ht=sra.getRequest();
        HttpSession se=ht.getSession();
        users us= (users) se.getAttribute("sn");
        lm.setUserId(us.getUserId());
        Date dt=new Date();
        //ServletRequestAttributes sra= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
      // logMsg lm= (logMsg) sra.getRequest().getSession().getAttribute("lmsg");
        System.out.println(lm.getVisitTime());
        lm.setExecTime((dt.getTime()-lm.getExecTime()));
        logmsgIm.add(lm);
    }

    //获取ip
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        String unknown = "unknown";
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }


}
